/**
 * tcp的工具类
 * @author 冯永泽
 * @date 2019/12/3
 */
let net = require('net');
class tcp {
    constructor(port) {
        this.tcp_server = net.createServer();
        this.tcpClient = net.Socket();
        this.port = port;
        this.Sockets = {};
        this.SocketID = 1;
    }
    /**
     * tcp开启监听
     *
     * @param {Number} port 端口
     * @param {Function} startFunction 开启监听后监听方法
     */
    tcpStart (startFunction = function() {console.log('tcp_server start')}) {
        this.tcp_server.listen(this.port,startFunction);
    }
    /**
     * 当有client连接时回调函数
     * @param {Function} connectionFunction 
     */
    tcpConnection (connectionFunction) {
        this.tcp_server.on('connection',function (socket){
            // this.Sockets = {};
            // this.Sockets[this.SocketID] = socket;
            // this.SocketID ++;
            connectionFunction(socket);
        })
    }
    /**
     *  tcp连接出错回调函数
     *  @param {Function} errorFunction 
     */
    tcpError (errorFunction = function(res) {console.log(res)}) {
        this.tcp_server.on('error', errorFunction);
    }
    /**
     * tcp连接关闭回调函数
     * @param {Function} closeFunction 
     */
    tcpClose(closeFunction = function() {console.log('tcp_server close!')}) {
        this.tcp_server.on('close',closeFunction);
    }
    /**
     * 向Tcp广播消息方法
     * @param {Object} socket tcp client对象 
     * @param {Function} dataFunction 
     */
    socketData(socket, dataFunction) {
        socket.on('data',function(data){
            data = data.toString();
            // 向所有客户端广播消息
        //    for(var i in this.Sockets){
        //        this.Sockets[i].write(data);
        //    }
            // socket.write(data);
            // console.log('received data %s',data);
            dataFunction(data);
        })
    }
    /**
     * 当tcp client关闭时回调函数
     * @param {Object} socket 
     * @param {Function} closeFunction 
     */
    socketClose(socket, closeFunction = function() {console.log('client close!')}) {
        socket.on('close', closeFunction);
    }
    /**
     * 当tcp client出错时回调函数
     * @param {Object} socket 
     * @param {Function} errorFunction 
     */
    socketError(socket, errorFunction = function() {console.log('client error!')}) {
        socket.on("error", errorFunction);
    }
    /**
     * 监听tcp client多久没通信
     *
     * @param {Object} socket 
     * @param {String} time 多少秒
     * @param {Function} cb 回调方式
     * @memberof tcp
     */
    socketTimeout(socket, time, cb) {
        socket.setTimeout(1000 * time, cb);
    }
    /**
     * TCP客户端连接
     *
     * @param {object} options 配置方法
     * @param {*} cb 连接成功回调方法
     * @memberof tcp
     */
    clientConnect(options, cb) {
        // options = {
        //     host : '172.29.3.235',
        //     port : 19898
        // }
        this.tcpClient.connect(options, () => {
            cb();
        });
    }
    /**
     *  客户端发送字符串
     *  
     * @param {*} str 发送的字符串
     * @param {*} cb 回调方法
     * @memberof tcp
     */
    clientSend(str, cb) {
        this.tcpClient.write(str, cb);
    }
    clientError(cb) {
        this.tcpClient.on('error', () => {
            cb();
        });
    }
}
module.exports = tcp;